﻿Shader "Unlit/PointMatrixNoLight"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_Width ("Width",float) = 0
		_Height ("Height",float) = 0
	}
	SubShader
	{
//		Tags { "RenderType"="Transparent" "Queue"="Transparent"}
		Tags { "RenderType"="Queue"}
		LOD 100
		//Blend SrcAlpha OneMinusSrcAlpha
		//ZWrite off

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				UNITY_FOG_COORDS(1)
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			float _Width;
			float _Height;

			float4 _pointPos(float2 uv)
			{
				float dx = 1 / _Width;
				float dy = 1 / _Height;
				
				float _tpx = uv.x / dx;
				float _tpy = uv.y / dy;
				float px = floor(_tpx);
				float py = floor(_tpy);
			
				float tx = (_tpx - px) - 0.5;
				float ty = (_tpy - py) - 0.5;
				float td = tx*tx + ty*ty;
				if(td >= 0.23)
				{
					discard;
				}
			
				return float4(px,py,dx,dy);
			}

			float4 _getPoint(float4 pos)
			{
				float offx = pos.x * pos.z + pos.z / 2;
				float offy = pos.y * pos.w + pos.w / 2;
				return tex2D(_MainTex, float2(offx,offy));
			}
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				UNITY_TRANSFER_FOG(o,o.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				
				float4 pc = _pointPos(i.uv);
			
				// sample the texture
				//fixed4 col = tex2D(_MainTex, i.uv);
				float4 col = _getPoint(pc);
				
				if(col.a < 0.9)
				{
					discard;
				}else 
				{
					col.rgb = col.rgb / col.a;
				}
				
				// apply fog
				//UNITY_APPLY_FOG(i.fogCoord, col);
				return col;
			}
			ENDCG
		}
	}
}
